﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;

public partial class ucControl_ucChitiet : System.Web.UI.UserControl
{
    TblMenuParrentDAO menuPaDAO = new TblMenuParrentDAO(Global.CONNECTION_STRING);
    TblMenuChildDAO menuChildDAO = new TblMenuChildDAO(Global.CONNECTION_STRING);
    TblImageDAO imgDAO = new TblImageDAO(Global.CONNECTION_STRING);
    TblItemsDAO itemDAO = new TblItemsDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadMenuPa();
            LoadItemLeft();
            LoadDongXe();
            string id = Request.QueryString["id"];
            if (id != null)
            {
                LoadFormView();
                LoadImg();
                LoadDescription();
            }

            // update vote
            int Vote = 0;
            DataTable dt = imgDAO.GetImage_ByItemID(Global.ConvertToInt(id));
            foreach (DataRow dr in dt.Rows)
            {
                Vote = Global.ConvertToInt(dr["Vote"].ToString());
                Vote++;
            }
            itemDAO.Update_Vote(Vote, Global.ConvertToInt(id));
        }
    }

    private void LoadItemLeft()
    {
        rpItemLEft.DataSource = itemDAO.GetItem_New();
        rpItemLEft.DataBind();
    }

    private void LoadMenuPa()
    {
        rpMenuPa.DataSource = menuPaDAO.GetAll();
        rpMenuPa.DataBind();
    }

    protected void rpMenuPa_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater reChildMenuLeft = (Repeater)e.Item.FindControl("rpmenuChild");
            HiddenField hidIDMenuPLeft = (HiddenField)e.Item.FindControl("hid");
            hidIDMenuPLeft.Value = DataBinder.Eval(e.Item.DataItem, "IDMenuParent").ToString();

            string id = hidIDMenuPLeft.Value;
            if (reChildMenuLeft != null)
            {
                reChildMenuLeft.DataSource = menuChildDAO.GetCateChild_ByIDParrent(Global.ConvertToInt(id));
                reChildMenuLeft.DataBind();
            }
        }
    }

    private void LoadFormView()
    {
        string id = Request.QueryString["id"];
        form.DataSource = imgDAO.GetImage_ByItemID(Global.ConvertToInt(id));
        form.DataBind();

        Label lb_KhuyenMai = (Label)form.FindControl("lbKhuyenMai");
        DataTable dt = imgDAO.GetImage_ByItemID(Global.ConvertToInt(id));
        string km = "";
        foreach (DataRow dr in dt.Rows)
        {
            km = dr["KhuyenMai"].ToString();
        }
        if (km == "" || km == null)
        {
            lb_KhuyenMai.Visible = false;
        }
        else
        {
            lb_KhuyenMai.Visible = true;
            lb_KhuyenMai.Text = "Khuyến mãi:" + " " + km;
        }
    }
    private void LoadImg()
    {
        string id = Request.QueryString["id"];
        DataTable dt = imgDAO.GetImage_ByItemID(Global.ConvertToInt(id));
        string listimgView1 = "";
        foreach (DataRow dr in dt.Rows)
        {
            listimgView1 = dr["ImageDetail"].ToString();
        }
        String[] listImg1 = listimgView1.Split(',');
        rpImageView.DataSource = listImg1;
        rpImageView.DataBind();
    }

    private void LoadDescription()
    {
        string id = Request.QueryString["id"];
        formDescription.DataSource = imgDAO.GetImage_ByItemID(Global.ConvertToInt(id));
        formDescription.DataBind();
    }
    
    private void LoadDongXe()
    {
        rpDongxe.DataSource = menuPaDAO.GetAll();
        rpDongxe.DataBind();
    }
    
}
